package com.taomu.app.zongchouhuli.utils.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.entity.OrderExplainEntity;
import com.taomu.app.zongchouhuli.utils.entity.OrderNumEntity;

import java.util.ArrayList;
import java.util.List;

public class OrderDialogBuyNumAdp extends RecyclerView.Adapter<OrderDialogBuyNumAdp.OrderDialogBuyNumHolder>{

    private List<OrderNumEntity> mData;

    private List<OrderNumEntity> mSelectData;

    private Context mContext;

    private LayoutInflater inflater;

    public OrderDialogBuyNumAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.mSelectData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<OrderNumEntity> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void setData(List<OrderNumEntity> mData,OrderNumEntity data){
        this.mData.clear();
        this.mData.addAll(mData);
        if(data != null){
            mSelectData.clear();
            mSelectData.add(data);
        }
        notifyDataSetChanged();
    }

    public void addData(List<OrderNumEntity> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public OrderDialogBuyNumHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_dialog_order_buy_num,parent,false);
        return new OrderDialogBuyNumHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderDialogBuyNumHolder holder, int position) {
        final OrderNumEntity entity = mData.get(position);
        holder.update(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkSelect(entity);
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class OrderDialogBuyNumHolder extends RecyclerView.ViewHolder{

        private TextView mNumTv;

        public OrderDialogBuyNumHolder(View itemView) {
            super(itemView);
            mNumTv = itemView.findViewById(R.id.item_dialog_order_buy_num_tv);
        }

        public void update(OrderNumEntity entity){
            mNumTv.setText(entity.getContent());
            if(isSelect(entity)){
                mNumTv.setBackgroundResource(R.drawable.shape_order_select_cornor);
            }else{
                mNumTv.setBackgroundResource(R.drawable.shape_order_unselect_cornor);
            }
        }
    }

    public void checkSelect(OrderNumEntity entity){
        mSelectData.clear();
        mSelectData.add(entity);
        notifyDataSetChanged();
    }

    public boolean isSelect(OrderNumEntity entity){
        return mSelectData.contains(entity);
    }

    public List<OrderNumEntity> getSelectData(){
        return mSelectData;
    }

    private OrderDialogBuyNumListener listener;

    public void setOrderDialogBuyNumListener(OrderDialogBuyNumListener listener){
        this.listener = listener;
    }

    public interface OrderDialogBuyNumListener{
        void onItemClick(OrderNumEntity entity);
    }
}
